package com.ruoyi.admin.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.admin.domain.TPatientFamily;
import com.ruoyi.admin.mapper.PatientMapper;
import com.ruoyi.admin.domain.Patient;
import com.ruoyi.admin.service.IPatientService;

/**
 * 患者列表Service业务层处理
 * 
 * @author Kpendragon
 * @date 2023-01-10
 */
@Service
public class PatientServiceImpl implements IPatientService 
{
    @Autowired
    private PatientMapper patientMapper;

    /**
     * 查询患者列表
     * 
     * @param id 患者列表主键
     * @return 患者列表
     */
    @Override
    public Patient selectPatientById(Long id)
    {
        return patientMapper.selectPatientById(id);
    }

    /**
     * 查询患者列表列表
     * 
     * @param patient 患者列表
     * @return 患者列表
     */
    @Override
    public List<Patient> selectPatientList(Patient patient)
    {
        return patientMapper.selectPatientList(patient);
    }

    /**
     * 新增患者列表
     * 
     * @param patient 患者列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPatient(Patient patient)
    {
        patient.setCreateTime(DateUtils.getNowDate());
        int rows = patientMapper.insertPatient(patient);
        insertTPatientFamily(patient);
        return rows;
    }

    /**
     * 修改患者列表
     * 
     * @param patient 患者列表
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePatient(Patient patient)
    {
        patientMapper.deleteTPatientFamilyByPatientId(patient.getId());
        insertTPatientFamily(patient);
        return patientMapper.updatePatient(patient);
    }

    /**
     * 批量删除患者列表
     * 
     * @param ids 需要删除的患者列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePatientByIds(Long[] ids)
    {
        patientMapper.deleteTPatientFamilyByPatientIds(ids);
        return patientMapper.deletePatientByIds(ids);
    }

    /**
     * 删除患者列表信息
     * 
     * @param id 患者列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePatientById(Long id)
    {
        patientMapper.deleteTPatientFamilyByPatientId(id);
        return patientMapper.deletePatientById(id);
    }

    /**
     * 新增查询关联家属信息
     * 
     * @param patient 患者列表对象
     */
    public void insertTPatientFamily(Patient patient)
    {
        List<TPatientFamily> tPatientFamilyList = patient.getTPatientFamilyList();
        Long id = patient.getId();
        if (StringUtils.isNotNull(tPatientFamilyList))
        {
            List<TPatientFamily> list = new ArrayList<TPatientFamily>();
            for (TPatientFamily tPatientFamily : tPatientFamilyList)
            {
                tPatientFamily.setPatientId(id);
                list.add(tPatientFamily);
            }
            if (list.size() > 0)
            {
                patientMapper.batchTPatientFamily(list);
            }
        }
    }
}
